package com.ruoyi.web.controller.applet;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.BdmUser;
import com.ruoyi.system.domain.dto.BdmUserDto;
import com.ruoyi.system.mapper.BdmUserMapper;
import com.ruoyi.system.service.IBdmUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.util.regex.Pattern;


/**
 * 用户Controller
 *
 * @author ruoyi
 * @date 2024-12-07
 */
@RestController
@RequestMapping("/applet/user")
@Validated
public class AppletUserController extends BaseController {
    @Autowired
    private IBdmUserService bdmUserService;

    @Autowired
    private BdmUserMapper bdmUserMapper;


    /**
     * 修改支付密码
     */
    @PutMapping("/putPaymentCode")
    public AjaxResult putPaymentCode(@Valid @NotBlank(message = "支付密码不能为空") @Size(max = 6, min = 6, message = "支付密码长度必须六位") String formerPayCode,
                                            @NotBlank(message = "支付密码不能为空") @Size(max = 6, min = 6, message = "支付密码长度必须六位") String newPayCode) {
        return toAjax(bdmUserService.putPaymentCode(formerPayCode,newPayCode));
    }

    /**
     * 修改支付密码
     */
    @GetMapping("/oneUser")
    public AjaxResult oneUser() {
        Long userId = getUserId();
        return success(bdmUserService.lambdaQuery().eq(BdmUser::getSysUserid, userId).one());
    }

    /**
     * 设置支付密码
     */
    @GetMapping("/setPassword")
    public AjaxResult setPassword(@Valid @NotBlank(message = "支付密码不能为空") @Size(max = 6, min = 6, message = "支付密码长度必须六位") @RequestParam("payCode") String payCode) {
        Long userId = getUserId();
        String passwordPattern = "^(?!.*(.).\\1\\1).*$";
        boolean isValidFormat = Pattern.matches(passwordPattern, payCode);
        if (!isValidFormat) {
            throw new RuntimeException("非法密码,不能出现连续四位相同重复密码");
        }
        return success(bdmUserService.lambdaUpdate().set(BdmUser::getPayCode, SecurityUtils.encryptPassword(payCode)).eq(BdmUser::getSysUserid, userId).update());
    }


    /**
     * 获取短信验证码
     */
    @GetMapping("/getNoteCode")
    public AjaxResult getNoteCode() {
        bdmUserService.getNoteCode();
        return success();
    }

    /**
     * 根据验证码修改支付密码
     */
    @PostMapping("/updatePayPassword")
    public AjaxResult updatePayPassword(@RequestBody @Validated BdmUserDto dto) {
        bdmUserService.updatePayPassword(dto);
        return success();
    }

    /**
     * 修改用户是否第一次绑定
     * @param bdmUser
     * @return
     */
    @PostMapping("/updateBdmUser")
    public AjaxResult updateBdmUser(@RequestBody BdmUser bdmUser) {
        bdmUserService.updateBdmUser(bdmUser);
        return success();
    }
}
